package swing.java2d;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DDZGame {
	
	List pai=new ArrayList();
	List a=new ArrayList();
	List b=new ArrayList();
	List c=new ArrayList();
	
	//一副牌
	public void init(){
		for(int i=1;i<5;i++){
			for(int j=3;j<=15;j++){
				Poker p=new Poker(i,j);
				pai.add(p);
			}
		}
		Poker king1=new Poker(5,88);
		Poker king2=new Poker(5,99);
		pai.add(king1);
		pai.add(king2);
		System.out.println(pai);
	}
	
	// 开局洗牌发牌
	public void newGame(){
		
	}
	
	// 检查牌型
	public CardState checkPai(int sz[]){
		Arrays.sort(sz);  //排序
		CardState cs=new CardState();
		cs.length= sz.length;  //牌的张数  默认是错误牌
	 
		switch (cs.length) {
		case 1:
			cs.cardType=CardType.SINGLE;
			cs.maxValue=sz[0];
			break;
		case 2:
			if(sz[0]==sz[1]){ //对子
				cs.cardType=CardType.DOUBLE;
				cs.maxValue=sz[0];
			}else if(sz[0]==88 && sz[1]==99){ //王炸
				cs.cardType=CardType.KING_BOMB;
			}
			break;
		case 3:
			if(sz[0]==sz[1] && sz[1]==sz[2]){
				cs.cardType=CardType.THREE;
				cs.maxValue=sz[0];
			}
			break;
		case 4:
			cs= checkBomb(sz);
			if(cs.cardType == CardType.ERROR_TYPE ){
				cs= check31(sz);
			}
			break;
		default:
			//检查连对
			cs= checkPlane(sz);
			break;
		}
		
		return cs;
	}
	
	//检查顺子
	private CardState  check12345(int sz[]){
		CardState cs=new CardState();
		cs.length= sz.length;  //牌的张数
		int n=1;
		for(int i=0;i<sz.length-1;i++){
			if(sz[i]+1==sz[i+1]){
				n++;
			}
		}
		if(n==cs.length){
			cs.cardType = CardType.SINGLE_SINGLE;
			cs.maxValue = sz[n-1];
		} 
		return cs;
	}
	
	//检查是不是炸
	private CardState  checkBomb(int sz[]){
		CardState cs=new CardState();
		cs.length= sz.length;  //牌的张数
		int n=1;
		for(int i=1;i<sz.length;i++){
			if(sz[i]==sz[0]){
				n++;
			}
		}
		if(n==4){
			cs.cardType = CardType.BOMB;
			cs.maxValue=sz[0];
		}  
		return cs;
	}
	
	//检查是不是3带1
	private CardState  check31(int sz[]){
		int n=0;
		CardState cs=new CardState();
		cs.length= sz.length;  //牌的张数
		if(sz[0]==sz[1]&&sz[1]==sz[2]){
			cs.cardType = CardType.THREE2ONE;
			cs.maxValue =sz[0];
		}
		if(sz[1]==sz[2]&&sz[2]==sz[3]){
			cs.cardType = CardType.THREE2ONE;
			cs.maxValue =sz[1];
		}
		return cs;
	}
	
	//检查连对  3 3 4 4 5 5 6 6
	private CardState  checkDoubleDouble(int sz[]){
		CardState cs=new CardState();
		int n=1;
		
		if(sz.length %2 ==0 && sz.length >= 6 ){  // 牌是偶数
			for(int i=0;i<=sz.length/2;i=i+2){
				if(sz[i]==sz[i+1]&&sz[i]+1==sz[i+2]){
					n++;
				}
			}
			if(n == sz.length/2){
				cs.cardType = CardType.DOUBLE_DOUBLE;
				cs.maxValue =sz[sz.length-1];
			}
		}
		return cs;
	}
	
	//检查飞机  
	// 1133 2233 111333 22223333 1111133333
	private CardState  checkPlane(int sz[]){
		CardState cs=new CardState();
		Map<Integer,Integer> map=new HashMap<Integer,Integer>();
		
		for(int i=0;i<sz.length;i++){
		  Integer n= map.get(sz[i]); //判断是否存在这个数
		  if (n==null) {
			  map.put(sz[i], 1);
		  }else{
			  map.put(sz[i], n+1);
		  }		
		}
		System.out.println(map);
		return cs;
	}
	
	
	public void show(CardState cs){
		System.out.println("-------分析结果-------");
		System.out.println("牌型是"+CardType.info[cs.cardType]);
		System.out.println("最大牌是"+cs.maxValue);
	}
	
	
	public static void main(String[] args) {
		/*List pai=new ArrayList();
		Poker p1=new Poker(1,3);
		Poker p2=new Poker(1,15);
		Poker p3=new Poker(1,4);
		Poker p4=new Poker(1,10);
		Poker p5=new Poker(1,12);
		
		pai.add(p1);
		pai.add(p2);
		pai.add(p3);
		pai.add(p4);
		pai.add(p5);
		
		Collections.sort(pai);
		System.out.println(pai);*/
		
		int sz[]={3,4,5,6,7};
		
		DDZGame game=new DDZGame();
		CardState cs= game.checkPai(sz);
		game.show(cs);
		 
		
	}

}
